import weather from "@/components/weather";
import Calender from "@/components/Calendar";
import request from "@/utils/request";
import home_echarts from "@/components/home_echarts";

export default {
    name: "Home",
    components: {
        weather,
        Calender,
        home_echarts,
    },
    data() {
        return {
            studentNum: "",
            haveRoomStudentNum: "",
            repairOrderNum: "",
            noFullRoomNum: "",
            activities: [],
            role: "",
            alertType: 'warning', // 默认类型
            alertDescription: '未缴纳水电费，请及时缴纳！', // 默认描述
            tableData: [], // 表格数据
            total: 0, // 总条数
            loading: false, // 加载状态
            currentPage: 1, // 当前页码
            pageSize: 10, // 每页条数
            search: '', // 搜索关键字
            date: [], // 日期范围
        };
    },
    created() {
        this.role = JSON.parse(window.sessionStorage.getItem("identity"));
        console.log(this.role);
        this.getHomePageNotice();
        this.getStuNum();
        this.getHaveRoomNum();
        this.getOrderNum();
        this.getNoFullRoom();
        this.load();
    },
    methods: {
        async load() {
            const currentUser = JSON.parse(sessionStorage.getItem("user")); // 获取当前登录用户信息
            const username = currentUser.username; // 假设用户信息中包含 username

            try {
                const res = await request.get("/pay/findstu/" + username, {
                    params: {
                        pageNum: this.currentPage,
                        pageSize: this.pageSize,
                        search: this.search,
                        date1: this.date[0] == null ? "" : this.date[0],
                        date2: this.date[1] == null ? "" : this.date[1],
                        username: username,
                    },
                });

                console.log(res);
                this.tableData = res.data.records;
                this.total = res.data.total;
                this.loading = false;

                // 检查是否有未缴费的账单
                const hasUnpaidBills = this.tableData.some(bill => bill.status === '未缴费');

                // 动态设置弹窗样式
                if (hasUnpaidBills) {
                    this.alertType = 'error';
                    this.alertDescription = '您有未缴纳的水电费，请及时缴纳！';
                } else {
                    this.alertType = 'success';
                    this.alertDescription = '您的水电费已全部缴纳！';
                }
            } catch (error) {
                console.error('加载数据失败', error);
                this.loading = false;
            }
        },
        async getStuNum() {
            request.get("/stu/stuNum").then((res) => {
                if (res.code === "0") {
                    this.studentNum = res.data;
                } else {
                    ElMessage({
                        message: res.msg,
                        type: "error",
                    });
                }
            });
        },
        async getHaveRoomNum() {
            request.get("/room/selectHaveRoomStuNum").then((res) => {
                if (res.code === "0") {
                    this.haveRoomStudentNum = res.data;
                } else {
                    ElMessage({
                        message: res.msg,
                        type: "error",
                    });
                }
            });
        },
        async getOrderNum() {
            request.get("/repair/orderNum").then((res) => {
                if (res.code === "0") {
                    this.repairOrderNum = res.data;
                } else {
                    ElMessage({
                        message: res.msg,
                        type: "error",
                    });
                }
            });
        },
        async getNoFullRoom() {
            request.get("/room/noFullRoom").then((res) => {
                if (res.code === "0") {
                    this.noFullRoomNum = res.data;
                } else {
                    ElMessage({
                        message: res.msg,
                        type: "error",
                    });
                }
            });
        },
        async getHomePageNotice() {
            request.get("/notice/homePageNotice").then((res) => {
                if (res.code === "0") {
                    this.activities = res.data;
                } else {
                    ElMessage({
                        message: res.msg,
                        type: "error",
                    });
                }
            });
        },
    },
};